import Vue from 'vue'
import Router from 'vue-router'
import mainPage from '@/view/mainPage/main-page.vue'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: '_index',
      redirect: '/index',
      component: mainPage,
      children: [
        {
          path: '/index',
          name: 'control-pannel',
          meta: {
            hideInMenu: true,
            title: '首页',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/controlPannel/control-pannel.vue')
        },
        {
          path: '/index/loan-management',
          name: 'loan-management',
          meta: {
            hideInMenu: true,
            title: '出借管理',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/LibraryManagement/LoanManagement/loan-management.vue')
        },
        {
          path: '/index/return-book',
          name: 'return-book',
          meta: {
            hideInMenu: true,
            title: '出借管理',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/LibraryManagement/returnBook/return-book.vue')
        },
        {
          path: '/index/advance-management',
          name: 'advance-management',
          meta: {
            hideInMenu: true,
            title: '预借管理',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/LibraryManagement/AdvanceManagement/advance-management.vue')
        },
        {
          path: '/index/borrowing-history',
          name: 'borrowing-history',
          meta: {
            hideInMenu: true,
            title: '借阅历史',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/LibraryManagement/BorrowingHistory/borrowing-history.vue')
        },
        {
          path: '/index/reader-information',
          name: 'readerInformation',
          meta: {
            hideInMenu: true,
            title: '读者信息',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/TheReader/readerInformation.vue')
        },
        {
          path: '/index/reader-identity',
          name: 'readerIdentity',
          meta: {
            hideInMenu: true,
            title: '读者身份',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/TheReader/readerIdentity.vue')
        },
        {
          path: '/index/book-list',
          name: 'bookList',
          meta: {
            hideInMenu: true,
            title: '书籍列表',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/addBook/bookList.vue')
        },
        {
          path: '/index/book-class',
          name: 'bookClass',
          meta: {
            hideInMenu: true,
            title: '书籍分类',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/addBook/bookClass.vue')
        },
        {
          path: '/index/book-press',
          name: 'bookPress',
          meta: {
            hideInMenu: true,
            title: '书籍分类',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/addBook/bookPress.vue')
        },
        {
          path: '/index/stores',
          name: 'storesList',
          meta: {
            hideInMenu: true,
            title: '门店',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/stores/storesList.vue')
        },
        {
          path: '/index/users',
          name: 'users',
          meta: {
            hideInMenu: true,
            title: '用户管理',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/systemSettings/users.vue')
        },
        {
          path: '/index/role',
          name: 'role',
          meta: {
            hideInMenu: true,
            title: '用户管理',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/systemSettings/role.vue')
        },
        {
          path: '/index/permissions',
          name: 'permissions',
          meta: {
            hideInMenu: true,
            title: '用户管理',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/systemSettings/permissions.vue')
        },
        {
          path: '/index/the-log',
          name: 'theLog',
          meta: {
            hideInMenu: true,
            title: '用户管理',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/systemSettings/theLog.vue')
        },
        {
          path: '/index/reset',
          name: 'resetPassword',
          meta: {
            hideInMenu: true,
            title: '重置密码',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/reset/resetPassword.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: 'Login - 登录'
      },
      component: () => import('@/view/login/login.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.hideInMenu) {
    if (localStorage.getItem('access_token')) {
      next()
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
})

export default router
